package jataaka.util;

import jataaka.command.URLCreatorCommand;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * Utility class for URL commands
 * 
 * @author Juan Carlos Cancela
 *
 */
public class URLCreatorCommandUtils {

	
	public static List<String> retrieveBaseCompositableURLs(URLCreatorCommand c)
	{
		if(isCommandValid(c)){
			return null;
		}
		List<String> compoundedURLs = new ArrayList<String>();	
		String baseUrl = createBaseURL(c);
		generateURLAdditionalInformation(c, compoundedURLs, baseUrl);
		
		return compoundedURLs;
		
	}

	/**
	 * validates the command
	 * 
	 * @param c
	 *            the command
	 * @return true or false
	 */
	private static boolean isCommandValid(URLCreatorCommand c) {
		return c.getHost() == null || c.getHost().equals("")
				|| c.getImmutableFolders() == null
				|| c.getMutableFolders() == null
				|| c.getImmutableParams() == null
				|| c.getMutableParams() == null;
	}


	/**
	 * creates the base url for all compounded urls. That is, it will
	 * concatenate the host with the list as string of mutable folders
	 * 
	 * @param c the command injected to the construction
	 * 
	 * @return the base url
	 */
	private static String createBaseURL(URLCreatorCommand c) 
	{
		String baseUrl = c.getHost() + getImmutableFoldersAsString(c);
		return baseUrl;
	}


	/**
	 * generates and attachs the additional information of the compounded URL, that is the mutable folders, and the mutable and inmutable parameters
	 * @param c the command injected to the construction
	 * @param compoundedURLs the list of compounded URLs
	 * @param baseUrl the base url
	 */
	private static void generateURLAdditionalInformation(URLCreatorCommand c,
			List<String> compoundedURLs, String baseUrl) {
		for(String mutableFolder : c.getMutableFolders()){
			String baseURlWithMF = baseUrl + mutableFolder + getImmutableParamsAsString(c);
			addMutableParamToCompoundedURL(c, compoundedURLs, baseURlWithMF);
		}
	}


	/**
	 * adds the specific mutable parameters to the compounded URL
	 * @param c the command
	 * @param compoundedURLs the list of compounded URLs
	 * @param baseURlWithMF the base URL with mutable folders
	 */
	private static void addMutableParamToCompoundedURL(URLCreatorCommand c,
			List<String> compoundedURLs, String baseURlWithMF) {
		for(String mutableParam : c.getMutableParams()){
			compoundedURLs.add(baseURlWithMF+mutableParam+".htm");
		}
	}
	
	/**
	 * get the list of immutable parameters of the URL
	 * @param c the command
	 * @return the list of immutable parameters 
	 */
	public static String getImmutableParamsAsString(URLCreatorCommand c){
		String immutableParams = "";
		for (String immutableParam : c.getImmutableParams()) {
			immutableParams = immutableParams + immutableParam;
		}
		return immutableParams;
	}

	/**
	 * get the list of inmutable folders as string
	 * @param c the command
	 * @return the list of immutable folders as string
	 */
	private static String getImmutableFoldersAsString(URLCreatorCommand c) {
		String immutableFolders = "";
		for (String immutableFolder : c.getImmutableFolders()) {
			immutableFolders = immutableFolders + immutableFolder;
		}
		return immutableFolders;
	}
}
